//
//  bookStoreViewController.m
//  诗韵
//
//  Created by Song on 2018/3/8.
//  Copyright © 2018年 Song. All rights reserved.
//

#import "bookStoreViewController.h"

#import "GSYTitleButton.h"

#import "chaodaiTableViewController.h"
#import "zuozheTableViewController.h"
#import "leixingTableViewController.h"
#import "shijiTableViewController.h"
#import "jiaocaiTableViewController.h"

@interface bookStoreViewController () <UIScrollViewDelegate>
- (IBAction)back:(id)sender;

@property (nonatomic,weak) UIScrollView *scrollView;

// 标题栏
@property(nonatomic,weak) UIView *titleView;
// 按钮底部指示器
@property(nonatomic,weak) UIView *indicatorView;
// 当前选中的按钮
@property(nonatomic,weak) GSYTitleButton *selectedTitleButton;

@end

@implementation bookStoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupChildViewControllers];
    [self setupScrollView]; // 左右滑动的scrollView
    [self setupTitlesView]; // 标题栏
    
    [self addChildVcView]; // 默认添加了一个子控制器的view（“全部”控制器）
    
//    self.view.backgroundColor = [UIColor yellowColor];
}


-(void)setupScrollView {
    // 不允许自动调整
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 84, self.view.frame.size.width, self.view.frame.size.height - 84)]; // 375 667
    
//    scrollView.backgroundColor = [UIColor greenColor];
    
    scrollView.pagingEnabled = YES; // 分页
//    scrollView.showsHorizontalScrollIndicator = NO; // 关闭水平滚动条的显示
//    scrollView.showsVerticalScrollIndicator = NO; // 关闭垂直滚动条的显示
    scrollView.contentSize = CGSizeMake(self.childViewControllers.count * scrollView.gsy_width, 0); // 只能左右滑动，不能上下滑动
    
    
    self.scrollView = scrollView;
    
    scrollView.delegate = self; // 代理
    
    [self.view addSubview:scrollView];
}

// 标题栏（五个按钮）
-(void)setupTitlesView {
    // 标题的view
    UIView *titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.7];
    titlesView.frame = CGRectMake(0, 84, self.view.gsy_width, 35); // 375
    self.titleView = titlesView;
    [self.view addSubview:titlesView];
    
    // 添加标题
    NSArray *titles = @[@"朝代",@"作者",@"类型",@"诗集",@"教材"];
    NSUInteger count = titles.count;
    CGFloat titleButtonW = titlesView.gsy_width / count;
    CGFloat titleButtonH = titlesView.gsy_height;
    for (NSUInteger i = 0; i < count; i++) {
        // 创建按钮
        GSYTitleButton *titleButton = [GSYTitleButton buttonWithType:UIButtonTypeCustom];
        titleButton.tag = i;
        [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [titlesView addSubview:titleButton];
        
        // 设置数据
        [titleButton setTitle:titles[i] forState:UIControlStateNormal];
        
        // 设置frame
        titleButton.frame = CGRectMake(i*titleButtonW, 0, titleButtonW, titleButtonH);
    }
    
    // 按钮的选中颜色
    GSYTitleButton *firstTitleButton = titlesView.subviews.firstObject;
    
    // 按钮底部的黑色指示器
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = [firstTitleButton titleColorForState:UIControlStateSelected];
    indicatorView.gsy_height = 2;
    indicatorView.gsy_y = titlesView.gsy_height - indicatorView.gsy_height;
    [titlesView addSubview:indicatorView];
    self.indicatorView = indicatorView;
    
    // 立刻根据文字内容算出label的宽度
    [firstTitleButton.titleLabel sizeToFit];
    indicatorView.gsy_width = firstTitleButton.titleLabel.gsy_width;
    indicatorView.gsy_centerX = firstTitleButton.gsy_centerX;
    
    // 默认情况：选中最前面的标题按钮
    firstTitleButton.selected = YES;
//    [self titleClick:firstTitleButton];
    self.selectedTitleButton = firstTitleButton;
}

// 添加子控制器
-(void)setupChildViewControllers {
    chaodaiTableViewController *chaodai = [[chaodaiTableViewController alloc] init];
    [self addChildViewController:chaodai];
    zuozheTableViewController *zuozhe = [[zuozheTableViewController alloc] init];
    [self addChildViewController:zuozhe];
    leixingTableViewController *leixing = [[leixingTableViewController alloc] init];
    [self addChildViewController:leixing];
    shijiTableViewController *shiji = [[shijiTableViewController alloc] init];
    [self addChildViewController:shiji];
    jiaocaiTableViewController *jiaocai = [[jiaocaiTableViewController alloc] init];
    [self addChildViewController:jiaocai];
}

// 标题栏的点击事件
-(void)titleClick:(GSYTitleButton *) titleButton {
    // 控制按钮状态
    self.selectedTitleButton.selected = NO;
    titleButton.selected = YES;
    self.selectedTitleButton = titleButton;
    
    // 按钮底部指示器
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.gsy_width = titleButton.titleLabel.gsy_width;
        self.indicatorView.gsy_centerX = titleButton.gsy_centerX;
    }];
    
    // 让UIScrollView滚动到对应位置
    CGPoint offset = self.scrollView.contentOffset;
    offset.x = titleButton.tag * self.scrollView.gsy_width;
    [self.scrollView setContentOffset:offset animated:YES];
}

#pragma mark - 添加子控制器的view
-(void)addChildVcView {
    // 子控制器的索引
    NSUInteger index = self.scrollView.contentOffset.x / self.scrollView.gsy_width;
    
    // 取出子控制器
    UIViewController *childVc = self.childViewControllers[index];
    
    if (childVc.view.superview) return; // 如果已经加载过一次，就直接返回
    //    if (childVc.view.window) return; // 同上
    //    if ([childVc isViewLoaded]) return; // 同上
    
    //    childVc.view.gsy_y = 0;
    //    childVc.view.gsy_x = index * self.scrollView.gsy_width;
    //    childVc.view.gsy_width = self.scrollView.gsy_width;
    //    childVc.view.gsy_height = self.scrollView.gsy_height;
    
    //    childVc.view.gsy_x = self.scrollView.contentOffset.x;
    //    childVc.view.gsy_y = self.scrollView.contentOffset.y;
    //    childVc.view.gsy_width = self.scrollView.gsy_width;
    //    childVc.view.gsy_height = self.scrollView.gsy_height;
    
    //    childVc.view.gsy_x = self.scrollView.bounds.origin.x;
    //    childVc.view.gsy_y = self.scrollView.bounds.origin.y;
    //    childVc.view.gsy_width = self.scrollView.bounds.size.width;
    //    childVc.view.gsy_height = self.scrollView.bounds.size.height;
    
    childVc.view.frame = self.scrollView.bounds;
    
    [self.scrollView addSubview:childVc.view];
}

#pragma mark - UIScrollViewDelegate
// 监听停止滚动
// 手动滑动停止滚动时：
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    // 选中/点击对应的按钮
    NSUInteger index = scrollView.contentOffset.x / scrollView.gsy_width;
    GSYTitleButton *titleButton = self.titleView.subviews[index];
    [self titleClick:titleButton];
    
    // 添加子控制器
    [self addChildVcView];
}

// 自动滑动（即点击了按钮）停止滚动时：
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    [self addChildVcView];
}


// back
- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"移除");
    }];
}
@end
